package com.server.rule.data.loader;



import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

import com.server.rule.data.Data;
import com.shared.rule.TypeDataRule;
import com.shared.data.IData;



public class InformationLoader {
	
	protected IData object;
	
	public InformationLoader(IData object) {
		this.object = object;
	}
	
	public void loadInformation(Data data) throws IllegalArgumentException, IllegalAccessException{
		
		
		Field[] fields = object.getClass().getDeclaredFields();
		AccessibleObject.setAccessible(fields, true);
		for(Field f : fields){			
			if(f.getType()==Double.class){				
				if(f.get(object)==null){
					data.put(getClassName()+"."+f.getName(),(Double) null, false);
				}else{
					Double s = f.getDouble(object);
					data.put(getClassName()+"."+f.getName(), s, false);
				}
			}else if(f.getType().getName().equals("boolean")){
				if(f.get(object)==null){
					data.put(getClassName()+"."+f.getName(),(Boolean) null, false);
				}else{
					Boolean s = f.getBoolean(object);
					data.put(getClassName()+"."+f.getName(), s, false);
				}
			}else if(f.getType()==String.class){				
				if(f.get(object)==null){
					data.put(getClassName()+"."+f.getName(),(String) null, false);
				}else{
					String s = (String) f.get(object);
					data.put(getClassName()+"."+f.getName(), s, false);
				}
			}else{}
		}		
	}
	
	
	
	public void loadInformationStructure(Data data) throws IllegalArgumentException, IllegalAccessException{
		object.getClass().getDeclaredFields();
		for(Field f :object.getClass().getDeclaredFields()){			
			if(f.getType()==Double.class){
				data.put(getClassName()+"."+f.getName(), (Double) null, false);
			}else if(f.getType().getName().equals("boolean")){
				data.put(getClassName()+"."+f.getName(), (Boolean) null, false);
			}else if(f.getType()==String.class){
				data.put(getClassName()+"."+f.getName(), (String) null, false);
			}else{}
		}
	}

	public String getClassName(){
		String c = object.getClass().getName();
		return c.substring(c.lastIndexOf(".")+1,c.length());
	}
	/**
	}
	 * @return the object
	 */
	public IData getObject() {
		return object;
	}


	/**
	 * @param object the object to set
	 */
	public void setObject(IData object) {
		this.object = object;
	}	
	
	public Map<String,TypeDataRule> getAvailableData() throws IllegalArgumentException, IllegalAccessException{
		Data dataTemp = new Data();
		loadInformationStructure(dataTemp);
		return dataTemp.getMapType();
	}
	
}
